package com.weiyi.mashangzhu.Landlord.Adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.okhttp.Request;
import com.weiyi.mashangzhu.API.API;
import com.weiyi.mashangzhu.Application.MyApp;
import com.weiyi.mashangzhu.Landlord.Activity.GatheringActivity;
import com.weiyi.mashangzhu.Landlord.Activity.MainActivity_L;
import com.weiyi.mashangzhu.R;
import com.weiyi.mashangzhu.Roomer.Activity.HouseDetailsActivity;
import com.weiyi.mashangzhu.Roomer.Activity.LogOutDialog;
import com.weiyi.mashangzhu.Roomer.Activity.OrderDetailActivity;
import com.weiyi.mashangzhu.Roomer.Bean.OrderBean;
import com.weiyi.mashangzhu.Utils.GlideImgManager;
import com.weiyi.mashangzhu.Utils.LoadingDialog;
import com.weiyi.mashangzhu.Utils.SPUtils;
import com.weiyi.mashangzhu.Utils.T;
import com.weiyi.mashangzhu.Weight.dialog.BoxDialog;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by win7 on 2017/5/12.
 */

public class OrderAdapter_L extends BaseAdapter {
    List<OrderBean.DataBean> list;
    Context context;
    String q,p;

    public OrderAdapter_L(List<OrderBean.DataBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    private void roomCard() {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        View view = View.inflate(context, R.layout.dialog_room_card, null);
        ImageButton close = (ImageButton) view.findViewById(R.id.card_close);
//        ImageView code = (ImageView) view.findViewById(R.id.card_code);
        builder.setView(view);
        final AlertDialog dialog = builder.show();
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    }

    private void showNormalDialog(final String order_id, final String status, final int position) {
        /* @setIcon 设置对话框图标
         * @setTitle 设置对话框标题
         * @setMessage 设置对话框消息提示
         * setXXX方法返回Dialog对象，因此可以链式设置属性
         */
        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(context);
        normalDialog.setIcon(R.mipmap.tishi);
        normalDialog.setTitle("提示");
        normalDialog.setMessage("你确定要取消此订单吗?");
        normalDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        OkHttpUtils.get().addParams("token", SPUtils.get(context, "token", "") + "")
                                .addParams("orderId", order_id + "")
                                .addParams("oStatus", status + "")
                                .url(API.BaseApi_L + API.DeleteOrder_Api)
                                .build()
                                .execute(new StringCallback() {
                                    @Override
                                    public void onError(Request request, Exception e) {
                                        T.showShort(context, "取消订单失败");
                                    }

                                    @Override
                                    public void onResponse(String response) {
                                        try {
                                            JSONObject jsonObject = new JSONObject(response);
                                            int status = jsonObject.getInt("status");
                                            if (status == 0) {
                                                list.remove(position);
                                                notifyDataSetChanged();
                                                T.showShort(context, "成功取消订单");
                                            } else {
                                                T.showShort(context, "取消订单失败");
                                            }
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }

                                    }
                                });


                    }
                });
        normalDialog.setNegativeButton("关闭",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //...To-do
                    }
                });
        // 显示
        normalDialog.show();
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_order_list, null);
            viewHolder.order_status = (TextView) convertView.findViewById(R.id.order_status);
            viewHolder.order_id = (TextView) convertView.findViewById(R.id.order_id);
            viewHolder.order_title = (TextView) convertView.findViewById(R.id.order_title);
            viewHolder.order_type = (TextView) convertView.findViewById(R.id.order_type);
            viewHolder.order_price = (TextView) convertView.findViewById(R.id.order_price);
            viewHolder.order_time = (TextView) convertView.findViewById(R.id.order_time);
            viewHolder.order_earnest = (TextView) convertView.findViewById(R.id.order_earnest);
            viewHolder.order_img = (ImageView) convertView.findViewById(R.id.order_img);
            viewHolder.order_blue_btn = (Button) convertView.findViewById(R.id.order_blue_btn);
            viewHolder.order_gray_btn = (Button) convertView.findViewById(R.id.order_gray_btn);
            viewHolder.order_line_btn = (Button) convertView.findViewById(R.id.order_line_btn);
            viewHolder.order_coupon = (TextView) convertView.findViewById(R.id.order_coupon);
            viewHolder.order_money = (TextView) convertView.findViewById(R.id.order_money);
//            viewHolder.order_coupon.setVisibility(View.VISIBLE);
//            viewHolder.order_coupon.setText("优惠券：满" + list.get(position).getCouponlist().get(0).get(0).getCQuota() + "减" + list.get(position).getCouponlist().get(0).get(0).getCPrice());
//        }


            convertView.setTag(viewHolder);

        }
        viewHolder = (ViewHolder) convertView.getTag();
        String status = "";
        switch (list.get(position).getOStatus()) {
            case 0:
                status = "待接单";
                viewHolder.order_coupon.setVisibility(View.VISIBLE);
                viewHolder.order_blue_btn.setVisibility(View.VISIBLE);
                viewHolder.order_gray_btn.setVisibility(View.VISIBLE);
                viewHolder.order_line_btn.setVisibility(View.GONE);
                viewHolder.order_blue_btn.setText("接单");
                viewHolder.order_gray_btn.setText("拒单");
//                viewHolder.order_line_btn.setText("联系房客");
                viewHolder.order_blue_btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //接单
                        LoadingDialog.show(context);
                        OkHttpUtils.get()
                                .addParams("token", SPUtils.get(context, "token", "") + "")
                                .addParams("id", list.get(position).getId() + "")
                                .addParams("operate", "1")
                                .url(API.BaseApi_L + API.ReceiveOrder)
                                .build()
                                .execute(new StringCallback() {
                                    @Override
                                    public void onError(Request request, Exception e) {
                                        LoadingDialog.dismiss(context);
                                        T.showShort(context, "数据加载失败，请稍后再试");
                                    }

                                    @Override
                                    public void onResponse(String response) {
                                        LoadingDialog.dismiss(context);
                                        Log.e("loooooooo", "re=" + response);
                                        if (response != null) {
                                            try {
                                                JSONObject jsonObject1 = new JSONObject(response);
                                                if (jsonObject1.getInt("status") == 0) {
                                                    T.showShort(context, "成功接受此订单");
                                                    list.remove(position);
                                                    notifyDataSetChanged();
//                                                    Intent intent = new Intent(context, MainActivity_L.class);
//                                                        intent.putExtra("info",1);
//                                                    context.startActivity(intent);
                                                } else {
                                                    T.showShort(context, "接受此订单失败");
                                                }
                                            } catch (JSONException e) {
                                                e.printStackTrace();
                                            }
                                        }

                                    }
                                });
                    }
                });
                viewHolder.order_gray_btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //拒单
                        LoadingDialog.show(context);
                        OkHttpUtils.get()
                                .addParams("token", SPUtils.get(context, "token", "") + "")
                                .addParams("id", list.get(position).getId() + "")
                                .addParams("operate", "0")
                                .url(API.BaseApi_L + API.ReceiveOrder)
                                .build()
                                .execute(new StringCallback() {
                                    @Override
                                    public void onError(Request request, Exception e) {
                                        LoadingDialog.dismiss(context);
                                        T.showShort(context, "数据加载失败，请稍后再试");
                                    }

                                    @Override
                                    public void onResponse(String response) {
                                        LoadingDialog.dismiss(context);
                                        Log.e("loooooooo", "re=" + response);
                                        if (response != null) {
                                            try {
                                                JSONObject jsonObject1 = new JSONObject(response);
                                                if (jsonObject1.getInt("status") == 0) {
                                                    list.remove(position);
                                                    notifyDataSetChanged();
                                                    T.showShort(context, "成功拒绝此订单");
                                                } else {
                                                    T.showShort(context, "拒绝此订单失败");
                                                }
                                            } catch (JSONException e) {
                                                e.printStackTrace();
                                            }
                                        }

                                    }
                                });
                    }
                });
//                viewHolder.order_line_btn.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        T.showShort(context,"联系房客");
//                    }
//                });
                break;
            case 1:
                status = "待付款";
                viewHolder.order_coupon.setVisibility(View.VISIBLE);
                viewHolder.order_blue_btn.setVisibility(View.VISIBLE);
                viewHolder.order_gray_btn.setVisibility(View.GONE);
                viewHolder.order_line_btn.setVisibility(View.GONE);
                viewHolder.order_blue_btn.setText("取消订单");
//                viewHolder.order_blue_btn.setText("联系房客");
//                viewHolder.order_blue_btn.setBackgroundResource(R.drawable.shape_gray_border_bg);
                viewHolder.order_blue_btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        showNormalDialog(list.get(position).getId() + "", list.get(position).getOStatus() + "", position);
                    }
                });

                break;
            case 2:
                status = "待入住";
//                viewHolder.order_blue_btn.setVisibility(View.VISIBLE);
//                viewHolder.order_gray_btn.setVisibility(View.VISIBLE);
//                viewHolder.order_line_btn.setVisibility(View.GONE);
                viewHolder.order_coupon.setVisibility(View.VISIBLE);
                viewHolder.order_blue_btn.setVisibility(View.VISIBLE);
                viewHolder.order_gray_btn.setVisibility(View.GONE);
                viewHolder.order_line_btn.setVisibility(View.GONE);
//                viewHolder.order_gray_btn.setText("取消订单");
                viewHolder.order_blue_btn.setText("房卡");
//                viewHolder.order_gray_btn.setText("联系房客");


//                viewHolder.order_gray_btn.setText("退款");
//                viewHolder.order_gray_btn.setBackgroundResource(R.drawable.shape_btn_line_bg);
//                viewHolder.order_blue_btn.setText("房卡");
                viewHolder.order_blue_btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(context, BoxDialog.class);
                        intent.putExtra("BuildNo",list.get(position).getHouse().getHBuilding()+"");
                        intent.putExtra("FloorNo",list.get(position).getHouse().getHFloor()+"");
                        intent.putExtra("RoomNo",list.get(position).getHouse().getHRoomnum()+"");
                        intent.putExtra("startTime",list.get(position).getOLivetime()+"");
                        intent.putExtra("endTime",list.get(position).getOCheckouttime()+"");
                        intent.putExtra("id",list.get(position).getId()+"");
                        context.startActivity(intent);
                    }
                });
                break;
            case 3:
                status = "入住中";
                viewHolder.order_coupon.setVisibility(View.VISIBLE);
                viewHolder.order_blue_btn.setVisibility(View.VISIBLE);
                viewHolder.order_gray_btn.setVisibility(View.GONE);
                viewHolder.order_line_btn.setVisibility(View.GONE);
//                viewHolder.order_gray_btn.setText("确认退房");
                viewHolder.order_blue_btn.setText("房卡");
                viewHolder.order_blue_btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(context, BoxDialog.class);
                        intent.putExtra("BuildNo",list.get(position).getHouse().getHBuilding()+"");
                        intent.putExtra("FloorNo",list.get(position).getHouse().getHFloor()+"");
                        intent.putExtra("RoomNo",list.get(position).getHouse().getHRoomnum()+"");
                        intent.putExtra("startTime",list.get(position).getOLivetime()+"");
                        intent.putExtra("endTime",list.get(position).getOCheckouttime()+"");
                        intent.putExtra("id",list.get(position).getId()+"");
                        context.startActivity(intent);
                    }
                });
                break;
            case 4:
                status = "待评价";
                viewHolder.order_coupon.setVisibility(View.VISIBLE);
                viewHolder.order_blue_btn.setVisibility(View.VISIBLE);
                viewHolder.order_gray_btn.setVisibility(View.GONE);
                viewHolder.order_line_btn.setVisibility(View.GONE);
//                viewHolder.order_blue_btn.setText("联系房客");
                viewHolder.order_blue_btn.setText("收款");
                viewHolder.order_blue_btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //收款
                        Intent intent=new Intent(context, GatheringActivity.class);
                        intent.putExtra("id",list.get(position).getId()+"");
                        context.startActivity(intent);
                    }
                });
                break;
            case 5:
                status = "退款中";
                viewHolder.order_coupon.setVisibility(View.VISIBLE);
                viewHolder.order_blue_btn.setVisibility(View.GONE);
                viewHolder.order_gray_btn.setVisibility(View.GONE);
                viewHolder.order_line_btn.setVisibility(View.GONE);
//                viewHolder.order_blue_btn.setText("联系房客");
//                viewHolder.order_gray_btn.setText("提款");
                break;
            case 6:
                status = "已退款";
                viewHolder.order_coupon.setVisibility(View.VISIBLE);
                viewHolder.order_blue_btn.setVisibility(View.GONE);
                viewHolder.order_gray_btn.setVisibility(View.GONE);
                viewHolder.order_line_btn.setVisibility(View.GONE);
//                viewHolder.order_blue_btn.setText("联系房客");
//                viewHolder.order_gray_btn.setText("提款");
                break;
            case 7:
                status = "收款中";
                viewHolder.order_coupon.setVisibility(View.VISIBLE);
                viewHolder.order_blue_btn.setVisibility(View.GONE);
                viewHolder.order_gray_btn.setVisibility(View.GONE);
                viewHolder.order_line_btn.setVisibility(View.GONE);
//                viewHolder.order_blue_btn.setText("联系房客");
//                viewHolder.order_gray_btn.setText("提款");
                break;
            case 8:
                status = "已收款";
                viewHolder.order_coupon.setVisibility(View.VISIBLE);
                viewHolder.order_blue_btn.setVisibility(View.GONE);
                viewHolder.order_gray_btn.setVisibility(View.GONE);
                viewHolder.order_line_btn.setVisibility(View.GONE);
//                viewHolder.order_blue_btn.setText("联系房客");
//                viewHolder.order_gray_btn.setText("提款");
                break;
            case 10:
                status = "已评价";
                viewHolder.order_coupon.setVisibility(View.VISIBLE);
                viewHolder.order_blue_btn.setVisibility(View.VISIBLE);
                viewHolder.order_gray_btn.setVisibility(View.GONE);
                viewHolder.order_line_btn.setVisibility(View.GONE);
//                viewHolder.order_blue_btn.setText("联系房客");
                viewHolder.order_blue_btn.setText("收款");
                viewHolder.order_blue_btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //收款
                        Intent intent=new Intent(context, GatheringActivity.class);
                        intent.putExtra("id",list.get(position).getId()+"");
                        context.startActivity(intent);
                    }
                });
                break;
        }
        viewHolder.order_status.setText(status + "");
        viewHolder.order_id.setText(list.get(position).getUNickname() + "");
        viewHolder.order_title.setText(list.get(position).getHouse().getHTitle() + "");
        viewHolder.order_type.setText(list.get(position).getHouse().getHRoom() + "居室");
        viewHolder.order_price.setText("¥" + list.get(position).getHouse().getHPrice() + "/晚");
        viewHolder.order_time.setText("入住期：" + list.get(position).getOLivetime() + "—" + list.get(position).getOCheckouttime());
        viewHolder.order_earnest.setText("定金：¥" + list.get(position).getODeposit());
        viewHolder.order_money.setText("订单总额：¥" + list.get(position).getOTotalPrice());
        GlideImgManager.glideLoader(context, API.BaseApi_url + MyApp.getUrlList5(list, position).get(0), R.mipmap.errer, viewHolder.order_img);
        if (list.get(position).getCouponlist().size() == 0) {
            viewHolder.order_coupon.setVisibility(View.GONE);
        } else if (list.get(position).getCouponlist().size() == 1) {
            q=list.get(position).getCouponlist().get(0).get(0).getCQuota()+"";
            p= list.get(position).getCouponlist().get(0).get(0).getCPrice()+"";
            viewHolder.order_coupon.setVisibility(View.VISIBLE);
            viewHolder.order_coupon.setText("优惠券：满" + list.get(position).getCouponlist().get(0).get(0).getCQuota() + "减" + list.get(position).getCouponlist().get(0).get(0).getCPrice());
        }
        final String finalStatus = status;
        final ViewHolder finalViewHolder = viewHolder;
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, OrderDetailActivity.class);
                intent.putExtra("id", list.get(position).getId() + "");
                intent.putExtra("info", "1");
                if (list.get(position).getCouponlist().size() == 1) {
                    intent.putExtra("cQuota",list.get(position).getCouponlist().get(0).get(0).getCQuota()+"");
                    intent.putExtra("cPrice",list.get(position).getCouponlist().get(0).get(0).getCPrice()+"");
                }
                intent.putExtra("status", finalViewHolder.order_status.getText()+"");

                context.startActivity(intent);
            }
        });

//        convertView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//              Intent intent=new Intent(context,HouseDetailsActivity.class);
//                intent.putExtra("id",list.get(position).getHouse().getId()+"");
//                context.startActivity(intent);
//            }
//        });
        return convertView;
    }

    class ViewHolder {
        TextView order_status, order_id, order_title, order_type, order_price, order_time, order_earnest, order_coupon, order_money;
        ImageView order_img;
        Button order_blue_btn, order_gray_btn, order_line_btn;
    }
}
